/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package UserInterface;

import Threads.QueueClient;
import ClientComm.Robot;
import ClientComm.ConnectionObject;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import ClientComm.Client;
import java.util.ArrayList;
import javax.swing.DefaultListModel;
import javax.swing.JList;

/**
 * CRIEPER Client user interface / CRIEPERClient View
 * 
 * @author Travis Shuff, Dakota Brown
 */
public class Client_View extends JFrame {

    private static Client client;
    private static ArrayList<Robot> robotArrayList;
    private JMenuBar menuBar = new JMenuBar();
    private JMenu fileMenu = new JMenu("File");
    private JMenuItem exitItem = new JMenuItem("Exit");
    private JMenuItem connectMenu = new JMenuItem("Connect");
    private JMenu optionsMenu = new JMenu("Options");
    private JMenuItem connectionSettingsMenuItem = new JMenuItem("Connection Settings");
    private JMenuItem videoSettingsMenuItem = new JMenuItem("Video Settings");
    private JMenuItem robotsBehaviorSettingsMenuItem = new JMenuItem("Robots and Behaviors");
    private JTextField statusBar = new JTextField();
    //ConnectObject for storing connection settings

    //setting dialogs
    private static ConnectionSettingsDialog connSett ;
    private static RobotsAndBehaviorSettingsDialog roboBehavior = new RobotsAndBehaviorSettingsDialog();
    private static VideoSettingsDialog videoSettings = new VideoSettingsDialog();

   
    //Client_View default constructor

    public Client_View() {

        client = new Client(this);
       
        connSett= new ConnectionSettingsDialog(client.connObj);

        setSize(800, 600);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setTitle("CRIEPER");
        setLayout(new BorderLayout());

        add(menuBar, BorderLayout.NORTH);
        add(statusBar, BorderLayout.SOUTH);
        menuBar.add(fileMenu);
        fileMenu.add(exitItem);
        fileMenu.add(connectMenu);
        menuBar.add(optionsMenu);
        optionsMenu.add(connectionSettingsMenuItem);
        optionsMenu.add(robotsBehaviorSettingsMenuItem);
        optionsMenu.add(videoSettingsMenuItem);

        
        
        
        //adds 
        connectionSettingsMenuItem.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                connSett.setVisible(true);
            }
        });



        //adds ActionListener to Robots and Behaviors Menu Item
        robotsBehaviorSettingsMenuItem.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                roboBehavior.setVisible(true);
            }
        });


        //adds ActionListener to Video Settings Menu Item
        videoSettingsMenuItem.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                videoSettings.setVisible(true);
            }
        });

        exitItem.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                if (client.connObj != null) {
                    client.connObj.closeConnection();
                    if (QueueClient.run) {
                        QueueClient.run = false;
                    }
                    displayStatus("Disconnected");
                } else {
                    displayStatus("Check Connection Settings");
                }

            }
        });

        connectMenu.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {          
                client.connect();
            }
        });

        setVisible(true);

    } //end of Client_View Constructor

    public static void main(String[] args){
         new Client_View();
    }
   
    
    
    /**
     * Displays message to status bar
     * 
     * @param status string to be displayed in the status bar
     * @since 10/9/11
     */
    public void displayStatus(String status) {
        statusBar.setText(status);
        validate();
    }



    /**
     * Connection Settings Window
     * 
     * @since 10/6/11
     */
    private static class ConnectionSettingsDialog extends JFrame {

        //Connection Setting variables
        //UI Variables
        private JPanel layoutPanel = new JPanel();
        private JLabel connLabel = new JLabel("Host Address: ");
        private JTextField hostAdd = new JTextField(15);
        private JLabel nameLabel = new JLabel("User Name: ");
        private JTextField userName = new JTextField(15);
        private JLabel passLabel = new JLabel("Password: ");
        private JPasswordField password = new JPasswordField(15);
        private JLabel portLabel = new JLabel("Port: ");
        private JTextField portNum = new JTextField(4);
        private JButton submit = new JButton("Submit");

        public ConnectionSettingsDialog(ConnectionObject conObj) {

            //loads prior connect settings if object is not null pointer
            if (conObj != null) {
                hostAdd.setText(conObj.getHost());
                userName.setText(conObj.getUser());
                password.setText(conObj.getPass());
                portNum.setText(conObj.getPort());
            } else {
                hostAdd.setText("xenoserver.dnsalias.com/crieplite");
                userName.setText("Dakota");
                password.setText("password");
                portNum.setText("22");
            }

            setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
            layoutPanel.setLayout(new GridLayout(0, 2));
            layoutPanel.add(connLabel);
            layoutPanel.add(hostAdd);
            layoutPanel.add(nameLabel);
            layoutPanel.add(userName);
            layoutPanel.add(passLabel);
            layoutPanel.add(password);
            layoutPanel.add(portLabel);
            layoutPanel.add(portNum);

            //adds ActionListener to 
            submit.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    client.connObj = new ConnectionObject(hostAdd.getText(), userName.getText(),
                            new String(password.getPassword()), portNum.getText());
                    setVisible(false);
                }
            });
            layoutPanel.add(submit);

            setContentPane(layoutPanel);
            setLocationRelativeTo(null);
            pack();
            setVisible(false);
        }

        /**
         * Gets new ConnectionObject
         * 
         * @return new ConnectionObject from current settings
         * @since 10/6/11
         */
        public ConnectionObject getConnectSettings() {
            return new ConnectionObject(hostAdd.getText(), userName.getText(),
                    new String(password.getPassword()), portNum.getText());
        }
    } // end ConnectionSettingsDialog

    /**
     * 
     * @since 10/4/11
     */
    private static class RobotsAndBehaviorSettingsDialog extends JFrame {
            JList robotList = new JList();
            DefaultListModel robotModel = new DefaultListModel();
            
        public RobotsAndBehaviorSettingsDialog() {
            setSize(300, 300);
            setLocationRelativeTo(null);
            robotList.setModel(robotModel);
            setVisible(false);
            setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
            refreshRobotList();
        }
        
        public void refreshRobotList(){
            robotModel.removeAllElements();
            robotArrayList = client.robotList;
            for(Robot i : robotArrayList)
                robotModel.addElement((i.getId()));
        }
    }//end RobotsAndBehaviorSettingsDialog

    /**
     * 
     * @since 10/4/11
     */
    private static class VideoSettingsDialog extends JFrame {

        public VideoSettingsDialog() {
            setSize(300, 300);
            setLocationRelativeTo(null);
            setVisible(false);
            setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
        }
    }
} // end VideoSettingsDialog
